package com.zeng.vueserve.coreservice.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.vueserve.coreservice.mapper.RoleMapper;
import com.zeng.vueserve.coreservice.entity.Role;
import com.zeng.vueserve.coreapi.vo.RoleVo;
import com.zeng.vueserve.coreapi.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public RoleVo addOne(RoleVo roleVo) {
    Role role = Role.builder().build();
    BeanUtils.copyProperties(roleVo, role, IgnoreStringUtil.ignoreExclude());
    int insert = roleMapper.insert(role);
    return BeanUtil.beanToVo(role, roleVo);
    }

    @Override
    public RoleVo getOneByCode(String code) {

    Role role = this.roleMapper.selectById(code);
    RoleVo roleVo = BeanUtil.beanToVo(role, new RoleVo());

    return roleVo;
    }

    @Override
    public Integer update(RoleVo roleVo) {
    Role role = BeanUtil.VoToBean(roleVo,
    new Role(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.roleMapper.updateById(role);
    }

    @Override
    public Integer delete(String code) {
    return this.roleMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.roleMapper.deleteBatchIds(codes);
    }

    @Override
    public List<RoleVo> findPage(Page page, RoleVo roleVo) {
        Role role = BeanUtil.VoToBean(roleVo, new Role());
        this.roleMapper.selectPage(page, this.getWrapper(roleVo));
        List orders = page.getRecords();
        List<RoleVo> roleVos = new ArrayList<>();
        orders.forEach(e->{
            roleVos.add(BeanUtil.beanToVo(e, new RoleVo()));
        });
        page.setRecords(null);
        return roleVos;
        }


    @Override
    public Integer count(RoleVo roleVo) {
        Integer integer = this.roleMapper.selectCount(this.getWrapper(roleVo));
        return integer;
    }

    public AbstractWrapper getWrapper(RoleVo roleVo) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (roleVo != null) {


        }
        return wrapper;
    }
}
